
	package br.uniminas.persistencia;

	import java.util.List;
	import org.hibernate.Query;
	import org.hibernate.Session;
	import org.hibernate.Transaction;
import br.uniminas.entidades.Convenio;


	public class ConvenioDao {

		private List<Convenio> listaConvenios;
		private Convenio convenio;
		private Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();

		public List todosConvenio() {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				listaConvenios = session.createQuery("from Convenio").list();
				return listaConvenios;
			} finally {
				session.close();
			}
		}

		public List getAllConvenio() {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				listaConvenios = session.createQuery("from Convenio").list();
				return listaConvenios;
			} finally {
				session.close();
			}
		}

		
		
		public Convenio getConvenio(Integer id) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Convenio as e where e.id =:id");
				q.setInteger("id", id);
				return (Convenio) q.uniqueResult();
			} finally {
				session.close();
			}
		}

		public List<Convenio> buscaConveniosPorCodigo(String codigo) {
			System.out.println(codigo);
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Convenio as f where f.codigo like :codigo");
				q.setString("codigo", "%"+codigo+"%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}
		
		

		public List<Convenio> buscaConvenioPorCidade(String cidade) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session
						.createQuery("from Convenio as f where f.cidade = :cidade");
				q.setString("cidade", "%" + cidade + "%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}

		public List<Convenio> buscaConvenioPorNome(String nomeFantasia) {
			System.out.println(nomeFantasia);
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session
						.createQuery("from Convenio as f where f.nomeFantasia like :nomeFantasia");
				q.setString("nomeFantasia", "%" + nomeFantasia + "%");
				List l = q.list();
				return l;
			} finally {
				session.close();
			}
		}

		public void criaRegistroConvenio(Convenio conven) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.save(conven);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}

		public Convenio pegaConvenio(String codigo) {
			session = HibernateUtil.getSessionFactory().openSession();
			try {
				session.beginTransaction();
				Query q = session.createQuery("from Convenio as e where e.codigo =:codigo");
				q.setString("codigo", codigo);
				return (Convenio) q.uniqueResult();
			} finally {
				session.close();
			}
		}
		
		
		
		public void deleteConvenio(Integer id) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				convenio = (Convenio) session.get(Convenio.class, id);
				session.delete(convenio);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}

		public void updateConvenio(Convenio conven) {
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = null;
			try {
				tx = session.beginTransaction();
				session.update(conven);
				tx.commit();
			} catch (RuntimeException e) {
				if (tx != null)
					tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}
	}

